import { revocationQuotationApi } from "@/api/waybillManagement/businessWaybill/waybillQuotation";
import { message } from "ant-design-vue";
import { Ref } from "vue";

export default function OperationHook(state: { drawerOpts: Base.DrawerOpts; passData: Ref<WaybillQuotation.ListRows>; dialogOpts: Base.DialogOpts; formState: Ref<any>; emit: any }) {
  const { drawerOpts, dialogOpts, passData, formState, emit } = state;
  // 查看发票
  const showInvoiceHandle = () => {
    drawerOpts.width = "1200px";
    drawerOpts.title = "查看发票";
    drawerOpts.type = 1;
    drawerOpts.passData = { id: passData.value.shipmentId, ...passData.value };
    drawerOpts.component = "ShowInvoice";
    drawerOpts.visible = true;
  };
  // 修改调整单价
  const changeAdjustUnitPriceHandle = () => {
    if (formState.value.freightCost.opType === "1") {
      formState.value.freightCost.clearingUnitPrice = formState.value.freightCost.unitPrice + formState.value.freightCost.adjustUnitPrice;
    } else {
      formState.value.freightCost.clearingUnitPrice = formState.value.freightCost.unitPrice - formState.value.freightCost.adjustUnitPrice;
    }
  };
  // 修改客户备注
  const modifyRemarkHandle = () => {
    dialogOpts.title = `修改客户备注`;
    dialogOpts.width = "520px";
    dialogOpts.type = 1;
    dialogOpts.component = "ModifyRemark";
    dialogOpts.passData = formState.value.bizPreOrderShipmentResDto;
    dialogOpts.visible = true;
  };
  // 修改客户备注
  const modifyInnerRemarkHandle = () => {
    dialogOpts.title = `修改内部备注`;
    dialogOpts.width = "520px";
    dialogOpts.type = 2;
    dialogOpts.component = "ModifyRemark";
    dialogOpts.passData = formState.value.bizPreOrderShipmentResDto;
    dialogOpts.visible = true;
  };

  // 查看材积明细
  const showBoxInfoHandle = () => {
    drawerOpts.width = "1200px";
    drawerOpts.title = "查看材积明细";
    drawerOpts.type = 1;
    drawerOpts.passData = passData.value;
    drawerOpts.component = "BoxInfo";
    drawerOpts.visible = true;
  };
  const revocationQuotationHandle = () => {
    return revocationQuotationApi({ id: passData.value.orderShipmentQuotationId }).then((res: any) => {
      const { code } = res;
      if (code === 200) {
        message.success("操作成功!");
        emit("submit");
      }
    });
  };

  // 合并报关详情
  const cancelMergeDeclarationHandle = () => {
    dialogOpts.title = `取消合并报关`;
    dialogOpts.width = "500px";
    dialogOpts.type = 3;
    dialogOpts.component = "CustomsClearance";
    dialogOpts.passData = [passData.value];
    dialogOpts.visible = true;
  };

  return {
    showInvoiceHandle,
    changeAdjustUnitPriceHandle,
    modifyRemarkHandle,
    modifyInnerRemarkHandle,
    showBoxInfoHandle,
    revocationQuotationHandle,
    cancelMergeDeclarationHandle
  };
}
